package com.achao.basecommon.utils;

import com.achao.basecommon.annotation.CustomValidation;
import com.achao.basecommon.exception.MusicCommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;

/**
 * @Description：校验器实现
 * @date：2023/8/3 15:29
 */
public class CustomValidatorUtils {
    /**
     *  @Description：参数校验(支持实体对象)
     *  @Param：任意实体对象
     *  @Returen：void
     */
    public static void validate(Object obj){
        if(null == obj){
            throw new MusicCommonException("校验对象为null");
        }
        //1->获取反射对象
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            //2->查看该对象属性上是否加了指定注解
            if (field.isAnnotationPresent(CustomValidation.class)) {
                //3->获取注解
                CustomValidation customValidation = field.getAnnotation(CustomValidation.class);
                //4->得到注解元素value值  推荐用正则校验
                String regex = customValidation.value();
                //5->允许访问私有属性
                field.setAccessible(true);
                Object value;
                try {
                    //6->得到私有属性
                    value = field.get(obj);
                } catch (IllegalAccessException e) {
                    throw new MusicCommonException("Illegal access to parameter " + field.getName());
                }
                //适用Integer 和 List
                if(null == value){
                    throw new MusicCommonException(customValidation.errorMessage());
                }
                if ((value instanceof String)) {
                    String str = (String) value;
                    if (StringUtils.isEmpty(str)) {
                        throw new MusicCommonException(customValidation.errorMessage());
                    }
                    //str正则校验
                    if(StringUtils.isNotEmpty(regex)){
                        checkMatch(str,regex,customValidation.fieldRemark());
                    }
                }
                if((value instanceof Integer)){
                    Integer intStr = (Integer) value;
                    //Integer正则校验
                    if(StringUtils.isNotEmpty(regex)){
                        checkMatch(intStr.toString(),regex,customValidation.fieldRemark());
                    }
                }
                if((value instanceof Collection)){
                    Collection collection = (Collection)value;
                    if(CollectionUtils.isEmpty(collection)){
                        throw new MusicCommonException(customValidation.errorMessage());
                    }
                }

            }
        }
    }

    /**
     *  @Description：正则校验
     *  @Param：targetStr(目标字符串)
     *  @Param：regex(正则语法)
     *  @Returen：boolean
     */
    private static void checkMatch(String targetStr,String regex,String reMark){
        boolean b =  targetStr.matches(regex);
        if(!b){
            throw new MusicCommonException("目标【" + reMark + "】不符合正则规则!!");
        }
    }
}
